layui.define(['jquery'], function(exports){
	var $ = layui.$, larryms = layui.larryms, layer = layui.layer;
	/** Ajax请求异常处理全局设置 */
	$.ajaxSetup({
		complete: function (xhr, status) {
			var time = 2000;
			if(404 == xhr.status){
				if(xhr.responseText){
					larryms.message("请求资源未找到！", 'error', time);
				}else {
					larryms.message("登录超时！", 'error', time);
					setTimeout(function () {
						top.location.href = "/login";
					}, time);
				}
			}else if(403 == xhr.status){
				larryms.message("权限不足！", 'error', time);
			}else if(500 == xhr.status){
				larryms.message("server 500 error", 'error', time);
			}else if(520 == xhr.status){
				larryms.message("登录超时！", 'error', time);
				setTimeout(function () {
					top.location.href = "/login";
				}, time);
			}
		}
	});
	var Beone = new Function();
	Beone.prototype.serializeObject = function (formId) {
		var o = {};
		var a = $("#" + formId).serializeArray();
		$.each(a, function () {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			} else {
				o[this.name] = this.value || '';
			}
		});
		return o;
	};

	Beone.prototype.clearForm = function (formId) {
		var o = {};
		var a = $("#" + formId).serializeArray();
		$.each(a, function () {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push('');
			} else {
				o[this.name] = '';
			}
			$("#" + this.name).val("");
		});
		return o;
	};

	Beone.prototype.dialog = function (opt) {
		var title = opt.title;
		if (!title) {
			title = false;
		}

		var url = opt.url;
		if (!url) {
			url = "/error/404";
		}

		var w = opt.width;
		if (!w) {
			w = ($(window).width() * 0.9);
		}

		var h = opt.height;
		if (!h) {
			h = ($(window).height() - 50);
		}
		return larryms.open({
			title: title,
			type: 2,
			shade: 0.4,
			area: [ w + 'px', h + 'px'],
			content: url,
			success: opt.success ? opt.success : function(){},
			end: opt.end ? opt.end : function(){}
		});
	};

	Beone.prototype.confirm = function (msg, callback) {
		return layer.confirm(msg, { btn: ['确认', '取消']},  callback)
	};

	Beone.prototype.toastMsg = function (opt) {
		var defaults = {content:'', icon:'info', anim:'default',time:1500};
		opt = $.extend(defaults, opt);
		var icon = 0, anim = 0;
		switch (opt.icon){
			case 'fail':
				icon = 5;
				break;
			case 'success':
				icon = 6;
				break;
		}
		switch (opt.anim){
			case 'fail':
				anim = 6;
				break;
			default:
				anim = 0;
		}
		layer.open({content: opt.content, closeBtn:0,title:'',btn:[],icon: icon,time:opt.time,anim: anim});
	};

	Beone.prototype.onEnter = function(callback){
		document.onkeydown = function (e) { // 回车提交表单
			var theEvent = window.event || e;
			var code = theEvent.keyCode || theEvent.which;
			if (code == 13) {
				if(callback && typeof callback == 'function'){
					callback();
				}
			}
		}
	};

	Beone.prototype.doRequest = function (url, data, callback) {
		$.ajax({
			url:url,
			type:"post",
			data:data,async:false,
			success:function(d){
				if(d.code == 'success'){
					if(callback && typeof callback == 'function'){
						callback();
					}
				}
				larryms.msg(d.desc);
			},error:function(){
				alert('error');
			}
		});
	};

	/**
	 * 返回特定格式的日期字串。
	 *
	 * @param pattern 返回的日期字串样式，例如 yyyy-mm-dd HH:MM:SS sss 返回 2009-11-10 09:40:58 456
	 *      y : 年
	 *      m : 月
	 *      d : 月里的天（1-31）
	 *      H : 天里的小时（0-23）
	 *      M : 小时里的分钟（0-59）
	 *      S : 秒
	 *      s : 毫秒
	 */
	Date.prototype.format = function (pattern) {
		var year, month, day, hour, min, sec, ms, isCn = false;
		if (pattern.indexOf('年') >= 0 || pattern.indexOf('月') >= 0
			|| pattern.indexOf('日') >= 0 || pattern.indexOf('号') >= 0) {
			isCn = true;
		}

		year = this.getFullYear();
		month = this.getMonth() + 1;
		day = this.getDate();
		hour = this.getHours();
		min = this.getMinutes();
		sec = this.getSeconds();
		ms = this.getMilliseconds();

		pattern = pattern.replace('yyyy', year);
		pattern = pattern.replace('mm', (isCn || month > 9) ? month : ('0' + month));
		if(pattern.indexOf("dd") != -1){
			pattern = pattern.replace('dd', (isCn || day > 9) ? day : ('0' + day));
		}

		if(pattern.indexOf("HH") != -1) {
			pattern = pattern.replace('HH', (isCn || hour > 9) ? hour : ('0' + hour));
		}
		if(pattern.indexOf("MM") != -1) {
			pattern = pattern.replace('MM', min > 9 ? min : ('0' + min));
		}
		if(pattern.indexOf("SS") != -1) {
			pattern = pattern.replace('SS', sec > 9 ? sec : ('0' + sec));
		}
		if(pattern.indexOf("sss") != -1) {
			pattern = pattern.replace('sss', (ms > 99 ? ms : (ms > 9 ? ('0' + ms) : ('00' + ms))));
		}

		return pattern;
	}

	String.prototype.toDate = function(){
		var converted = Date.parse(this);
		var myDate = new Date(converted);
		if (isNaN(myDate)) {
			var arys= DateStr.split('-');
			myDate = new Date(arys[0],arys[1],arys[2]);
		}
		return myDate;
	}

	Beone.prototype.datedifference = function(strStart, strEnd){
		var day = strEnd.toDate().getTime() - strStart.toDate().getTime();
		return Math.floor(day / (24 * 3600 * 1000));
	}

	Beone.prototype.addDate = function(strDate, days){
		var date = strDate.toDate();
		date.setDate(date.getDate() + days);
		return date.format("yyyy-mm-dd");
	}

	Beone.prototype.formatDate = function(millisecond, patternType) {
		if (millisecond == null) {
			return "";
		}
		var pattern = 'yyyy-mm-dd HH:MM:SS';
		switch (patternType) {
			case 'yyyy-mm-dd HH:MM':
				pattern = 'yyyy-mm-dd HH:MM';
				break;
			case 'yyyy-mm-dd':
				pattern = 'yyyy-mm-dd';
				break;
			case 'mm-dd HH:MM':
				pattern = 'mm-dd HH:MM';
				break;
			case 'yyyy-mm':
				pattern = 'yyyy-mm';
				break;
		}
		return new Date(millisecond).format(pattern);
	}

	var beone = new Beone();
	exports('custom', beone);
});